package com.jsmtr.rms.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jsmtr.rms.entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jsmtr.rms.service.IDoZZService;
import com.jsmtr.rms.service.IProjectService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.EnumTools;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/Do/ZZCampus")
public class DoZZCampusController {

    @Resource
    private IUtilService utilService;

    @Resource
    private IProjectService projectService;

    @Resource
    private IDoZZService doZZService;


    //[start] 招聘实施主界面
    @RequestMapping("/")
    public String Index(HttpServletRequest request, Model model) {
        try {
            model.addAttribute("path", request.getContextPath());
            List<ProjectInfo> projects = projectService.getProjectInfos(EnumTools.RecruitType.中专校园招聘.ordinal());
            StringBuilder pros = new StringBuilder();
            for (ProjectInfo project : projects) {
                if (project.getState() != EnumTools.ProjectState.编辑中.ordinal()) {
                    pros.append("<option value='" + project.getId() + "'>" + project.getPname() + "</option>");
                }
            }
            model.addAttribute("projects", pros.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/ZZCampus/index";
    }
    //[end]


    //region  获得招聘方案责任部门列表
    @RequestMapping("/getdept")
    public void getdept(HttpServletRequest request, HttpServletResponse response) {
        try {
            String pid = request.getParameter("pid").trim();
            List<ProjectInfoFlow> flows = null;

            BaseUser user = utilService.getSessionUser(request);
            StringBuilder pros = new StringBuilder();
            //当前用户是招聘主管部门
            if (utilService.getConfig("hrcode").equals(user.getUdept())) {
                flows = projectService.getAllFlowDept(pid, 0);
                for (ProjectInfoFlow flow : flows) {
                    pros.append("<option value='" + flow.getDeptcode() + "'>" + flow.getDeptname() + "</option>");
                }
            } else {
               // flows = projectService.getThisFlowDept(pid, user.getUdept());
                //pros.append("<option value='" + flows.get(0).getDeptcode() + "'>" + flows.get(0).getDeptname() + "</option>");
                flows = projectService.getAllFlowDept(pid, 0);
                for (ProjectInfoFlow flow : flows) {
                    pros.append("<option value='" + flow.getDeptcode() + "'>" + flow.getDeptname() + "</option>");
                }
            }

            utilService.printTextResult(pros.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("", response);
        }
    }
    //endregion[end]

    //region [start] 获得招聘实施流程信息
    @RequestMapping("/getflows")
    public void getFlows(HttpServletRequest request, HttpServletResponse response) {
        try {
            String pid = request.getParameter("pid").trim();
            String deptcode = request.getParameter("deptcode").trim();
            List<ProjectInfoFlow> flows = projectService.getThisFlowDept(pid, deptcode);
            StringBuilder result = new StringBuilder();
            int index = 1;
            for (ProjectInfoFlow flow : flows) {
                int ftype = flow.getFid();
                String url = null;
                if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowState.毕业生推荐.name())||flow.getFname().equalsIgnoreCase( EnumTools.FlowNomalState.简历筛选.name())) {
                    url = "ZZRecommend/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowState.初审.name())) {
                    url = "ZZReview/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowState.投简历.name())) {
                    url = "ZZResume/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowState.面试.name())) {
                    url = "ZZInterview/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowState.背景调查.name())) {
                    url = "ZZPReview/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (flow.getFname().equalsIgnoreCase(  EnumTools.ZZFlowState.体检.name())) {
                    url = "ZZPhysical/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
               /* } else if (ftype == EnumTools.ZZFlowState.签约.ordinal()) {
                    url = "ZZPost/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();*/
                } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowState.实习.name())) {
                    url = "ZZPractice/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowState.录用.name())) {
                    url = "ZZEmploy/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowState.签约.name())) {
                    url = "ZZSigning/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else {
                    url = "";
                }
                result.append("<div class='cont cont" + flow.getState() + "'>");
                result.append("<a href='" + request.getContextPath() + "/Do/" + url + "'>");
                result.append("<span class='num'>" + index + "</span>");
                result.append("<span class='txt'>" + flow.getNickname() + "</span>");
                result.append("</a></div>");
                index++;
            }
            result.append("<div class='clear'></div>");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            utilService.printTextResult("", response);
        }
    }
    //endregion [end]


    //region  [start] 获得招聘实施流程信息
    @RequestMapping(value = "/getFlowsJson_bak",produces = "application/json;charset=utf-8")
    public @ResponseBody
    String getFlowsJson(HttpServletRequest request, HttpServletResponse response) {
        try {

            String pid = request.getParameter("pid").trim();
            String deptcode = request.getParameter("deptcode").trim();
            List<ProjectInfoFlow> flows = projectService.getThisFlowDept(pid, deptcode);
            StringBuilder result = new StringBuilder();
            int index = 1;
            result.append("[");
            for (ProjectInfoFlow flow : flows) {
                int ftype = flow.getFid();
                String url = null;

                //if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowState.初审.name())
                if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowStateNew.毕业生推荐.name())||flow.getFname().equalsIgnoreCase( EnumTools.FlowNomalState.简历筛选.name())) {
                    if(pid.toUpperCase().indexOf("C_")>=0)//秋季
                        url = "ZZRecommend/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                    else
                        url = "ZZResume/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                /*} else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowStateNew.初审.name())) {
                    url = "ZZReview/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowStateNew.投简历.name())) {
                    url = "ZZResume/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();*/
                } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowStateNew.笔试.name())) {
                    url = "ZZExam/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowStateNew.资料审核.name())) {
                    url = "SZCheck/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowStateNew.面试.name())) {
                    url = "ZZInterview/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowStateNew.背景调查.name())) {
                    url = "ZZPReview/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (flow.getFname().equalsIgnoreCase(  EnumTools.ZZFlowStateNew.体检.name())) {
                    url = "ZZPhysical/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
               /* } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowStateNew..name())) {
                    url = "ZZPost/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();*/
               /* } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowStateNew.实习.name())) {
                    url = "ZZPractice/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();*/
                } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowStateNew.录用.name())) {
                    url = "ZZEmploy/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (flow.getFname().equalsIgnoreCase( EnumTools.ZZFlowStateNew.签约.name())) {
                    url = "ZZSigning/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();

                } else {
                    url = "";
                }
                String href=request.getContextPath() + "/Do/" + url;
                result.append("{\"fid\":\""+flow.getId()+"\",\"state\":\""+flow.getState()+"\",\"num\":\""+index+"\",\"txt\":\""+flow.getNickname()+"\",\"url\":\""+href+"\"},");
                index++;
            }
            result=result.replace(result.lastIndexOf(","),result.length(),"");
            result.append("]");

            //utilService.printTextResult(result.toString(), response);
            return result.toString();
        } catch (Exception e) {
            //utilService.printTextResult("", response);
            return "";
        }

    }
    //endregion

    //region [start] 公布阶段审核结果（已公布->未公布/未公布->已公布）
    @SuppressWarnings({"rawtypes", "unchecked"})
    @RequestMapping(value = "/PublishResultAction", method = RequestMethod.POST)
    public void PublishResultAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] ids = request.getParameterValues("id");//人员信息ID
            if (ids != null) {
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码
                BaseUser usr = utilService.getSessionUser(request);
                if (usr.getUdept().equals(deptcode)) {
                    //流程是否结束
                    if (utilService.FlowIsDoing(fid)) {
                        List editlist = new ArrayList();
                        List<HRZZUser> usrs = doZZService.getUsers(ids);
                        List<DUserResume> usrDs = doZZService.getUsersDuser(ids);
                        DUserResume usrDsTemp = new DUserResume();
                        ProjectInfoFlow nFlow = doZZService.getCurrentFlow(fid);
                        if (nFlow.getFname().equalsIgnoreCase( EnumTools.ZZFlowState.毕业生推荐.name())||nFlow.getFname().equalsIgnoreCase( EnumTools.FlowNomalState.简历筛选.name())) {
                            for (HRZZUser user : usrs) {
                                for(int i =0 ;i<usrDs.size(); i++){
                                    if(usrDs.get(i).getUserid().equals(user.getId())){
                                        usrDsTemp = usrDs.get(i);
                                        break;
                                    }
                                }
                                if (user.getOp1().equals("未公布")) {
                                    user.setOp1("已公布");
                                    usrDsTemp.setOp1("已公布");
                                }else {
                                    user.setOp1("未公布");
                                    usrDsTemp.setOp1("未公布");
                                }
                                    editlist.add(user);
                                    editlist.add(usrDsTemp);
                            }
                        }
                        if (nFlow.getFname().equalsIgnoreCase(EnumTools.ZZFlowState.面试.name())) {
                            for (HRZZUser user : usrs) {
                                for(int i =0 ;i<usrDs.size(); i++){
                                    if(usrDs.get(i).getUserid().equals(user.getId())){
                                        usrDsTemp = usrDs.get(i);
                                        break;
                                    }
                                }
                                if (user.getOp2().equals("未公布")){
                                    user.setOp2("已公布");
                                    usrDsTemp.setOp1("已公布");
                                }else{
                                    user.setOp2("未公布");
                                    usrDsTemp.setOp1("未公布");
                                }
                                editlist.add(user);
                                editlist.add(usrDsTemp);
                            }
                        }
                        if (nFlow.getFname().equalsIgnoreCase(EnumTools.ZZFlowState.投简历.name())) {
                            for (HRZZUser user : usrs) {
                                for(int i =0 ;i<usrDs.size(); i++){
                                    if(usrDs.get(i).getUserid().equals(user.getId())){
                                        usrDsTemp = usrDs.get(i);
                                        break;
                                    }
                                }
                                if (user.getOp3().equals("未公布")) {
                                    user.setOp3("已公布");
                                    usrDsTemp.setOp1("已公布");
                                }else {
                                    user.setOp3("未公布");
                                    usrDsTemp.setOp1("未公布");
                                }
                                editlist.add(user);
                                editlist.add(usrDsTemp);
                            }
                        }
                        if (nFlow.getFname().equalsIgnoreCase(EnumTools.ZZFlowState.背景调查.name())) {
                            for (HRZZUser user : usrs) {
                                for(int i =0 ;i<usrDs.size(); i++){
                                    if(usrDs.get(i).getUserid().equals(user.getId())){
                                        usrDsTemp = usrDs.get(i);
                                        break;
                                    }
                                }
                                if (user.getOp4().equals("未公布")) {
                                    user.setOp4("已公布");
                                    usrDsTemp.setOp1("已公布");
                                }else
                                {
                                    user.setOp4("未公布");
                                    usrDsTemp.setOp1("未公布");
                                }
                                editlist.add(user);
                                editlist.add(usrDsTemp);
                            }
                        }
                        if (nFlow.getFname().equalsIgnoreCase(EnumTools.ZZFlowState.体检.name())) {
                            for (HRZZUser user : usrs) {
                                for(int i =0 ;i<usrDs.size(); i++){
                                    if(usrDs.get(i).getUserid().equals(user.getId())){
                                        usrDsTemp = usrDs.get(i);
                                        break;
                                    }
                                }
                                if (user.getOp5().equals("未公布")) {
                                    user.setOp5("已公布");
                                    usrDsTemp.setOp1("已公布");
                                }else {
                                    user.setOp5("未公布");
                                    usrDsTemp.setOp1("未公布");
                                }
                                editlist.add(user);
                                editlist.add(usrDsTemp);
                            }
                        }
                        if (nFlow.getFname().equalsIgnoreCase(EnumTools.ZZFlowState.签约.name()) ) {
                            for (HRZZUser user : usrs) {
                                for(int i =0 ;i<usrDs.size(); i++){
                                    if(usrDs.get(i).getUserid().equals(user.getId())){
                                        usrDsTemp = usrDs.get(i);
                                        break;
                                    }
                                }
                                if (user.getOp9().equals("未公布")) {
                                    user.setOp9("已公布");
                                    usrDsTemp.setOp1("已公布");
                                }else {
                                    user.setOp9("未公布");
                                    usrDsTemp.setOp1("未公布");
                                }
                                editlist.add(user);
                                editlist.add(usrDsTemp);
                            }
                        }
                        if (nFlow.getFname().equalsIgnoreCase(EnumTools.ZZFlowState.实习.name())) {
                            for (HRZZUser user : usrs) {
                                for(int i =0 ;i<usrDs.size(); i++){
                                    if(usrDs.get(i).getUserid().equals(user.getId())){
                                        usrDsTemp = usrDs.get(i);
                                        break;
                                    }
                                }
                                if (user.getOp7().equals("未公布")) {
                                    user.setOp7("已公布");
                                    usrDsTemp.setOp1("已公布");
                                }else {
                                    user.setOp7("未公布");
                                    usrDsTemp.setOp1("未公布");
                                }
                                editlist.add(user);
                                editlist.add(usrDsTemp);
                            }
                        }
                        if (nFlow.getFname().equalsIgnoreCase( EnumTools.ZZFlowState.录用.name())) {
                            for (HRZZUser user : usrs) {
                                for(int i =0 ;i<usrDs.size(); i++){
                                    if(usrDs.get(i).getUserid().equals(user.getId())){
                                        usrDsTemp = usrDs.get(i);
                                        break;
                                    }
                                }
                                if (user.getOp8().equals("未公布")) {
                                    user.setOp8("已公布");
                                    usrDsTemp.setOp1("已公布");
                                }else {
                                    user.setOp8("未公布");
                                    usrDsTemp.setOp1("未公布");
                                }
                                editlist.add(user);
                                editlist.add(usrDsTemp);
                            }
                        }

                        if (editlist.size() > 0) {
                            utilService.editList(editlist);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中的信息不能执行此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "请选择人员信息！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }
    // endregion [end]

}
